Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8224 > unrolled thread
| Started by | cerr <ron.eggler@gmail.com> |
|---|---|
| First post | 2011-07-25 20:45 -0700 |
| Last post | 2011-07-26 22:41 +1200 |
| Articles | 12 — 5 participants |
Back to article view | Back to comp.lang.c++
simple pthread cerr <ron.eggler@gmail.com> - 2011-07-25 20:45 -0700
Re: simple pthread red floyd <no.spam.here@its.invalid> - 2011-07-25 20:55 -0700
Re: simple pthread Ian Collins <ian-news@hotmail.com> - 2011-07-26 15:56 +1200
Re: simple pthread "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-07-26 08:28 +0200
Re: simple pthread cerr <ron.eggler@gmail.com> - 2011-07-25 23:31 -0700
Re: simple pthread cerr <ron.eggler@gmail.com> - 2011-07-25 23:53 -0700
Re: simple pthread Ian Collins <ian-news@hotmail.com> - 2011-07-26 19:43 +1200
Re: simple pthread "io_x" <a@b.c.invalid> - 2011-07-26 09:57 +0200
Re: simple pthread "io_x" <a@b.c.invalid> - 2011-07-26 10:31 +0200
Re: simple pthread "io_x" <a@b.c.invalid> - 2011-07-26 12:38 +0200
Re: simple pthread Ian Collins <ian-news@hotmail.com> - 2011-07-26 22:39 +1200
Re: simple pthread Ian Collins <ian-news@hotmail.com> - 2011-07-26 22:41 +1200
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-25 20:45 -0700 |
| Subject | simple pthread |
| Message-ID | <0655071f-8f13-4456-af6a-69b80d0a21de@glegroupsg2000goo.googlegroups.com> |
hi There,
I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
$ g++ -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:42:59: error: expected primary-expression before ‘void’
example.cpp:42:63: error: initializer expression list treated as compound expression
the code:
int MyThread::StartMe(void)
{
int pthread_create(&ThreadA, NULL, &MyThread::PrintMsg, void);
}
//-------------------------------------------------------------
void MyThread::PrintMsg(void)
{
cout << "I Incremented count: " << count << endl;
}
Can anyone point it out? I'm like HUH???
Thanks,
[toc] | [next] | [standalone]
| From | red floyd <no.spam.here@its.invalid> |
|---|---|
| Date | 2011-07-25 20:55 -0700 |
| Message-ID | <j0ldrc$7kq$1@dont-email.me> |
| In reply to | #8224 |
On 7/25/2011 8:45 PM, cerr wrote:
> hi There,
>
> I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
> $ g++ -o example example.cpp
> example.cpp: In member function ‘int MyThread::StartMe()’:
> example.cpp:42:59: error: expected primary-expression before ‘void’
> example.cpp:42:63: error: initializer expression list treated as compound expression
> the code:
> int MyThread::StartMe(void)
> {
> int pthread_create(&ThreadA, NULL,&MyThread::PrintMsg, void);
get rid of "int"
> }
> //-------------------------------------------------------------
>
> void MyThread::PrintMsg(void)
> {
> cout<< "I Incremented count: "<< count<< endl;
> }
>
> Can anyone point it out? I'm like HUH???
>
> Thanks,
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-26 15:56 +1200 |
| Message-ID | <996s77FkjuU12@mid.individual.net> |
| In reply to | #8224 |
On 07/26/11 03:45 PM, cerr wrote:
> hi There,
>
> I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
> $ g++ -o example example.cpp
> example.cpp: In member function ‘int MyThread::StartMe()’:
> example.cpp:42:59: error: expected primary-expression before ‘void’
void is a type, you can't pass a type as a parameter!
> example.cpp:42:63: error: initializer expression list treated as compound expression
> the code:
> int MyThread::StartMe(void)
> {
> int pthread_create(&ThreadA, NULL,&MyThread::PrintMsg, void);
This looks like a mixed up function declaration!
int something = pthread_create( );
A decent compiler should tell you that MyThread::PrintMsg is the wrong
function type for the function pointer parameter of pthread_create.
> }
> //-------------------------------------------------------------
>
> void MyThread::PrintMsg(void)
> {
> cout<< "I Incremented count: "<< count<< endl;
> }
>
> Can anyone point it out? I'm like HUH???
>
> Thanks,
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Date | 2011-07-26 08:28 +0200 |
| Message-ID | <j0lmqr$em0$1@dont-email.me> |
| In reply to | #8224 |
On 26.07.2011 05:45, cerr wrote:
>
> I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
> $ g++ -o example example.cpp
> example.cpp: In member function ‘int MyThread::StartMe()’:
> example.cpp:42:59: error: expected primary-expression before ‘void’
> example.cpp:42:63: error: initializer expression list treated as compound expression
> the code:
> int MyThread::StartMe(void)
> {
> int pthread_create(&ThreadA, NULL,&MyThread::PrintMsg, void);
`void` is a type.
Cheers & hth.,
- Alf
[toc] | [prev] | [next] | [standalone]
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-25 23:31 -0700 |
| Message-ID | <9adbecbc-a4fa-47d0-a667-040a36f0fd7f@28g2000pry.googlegroups.com> |
| In reply to | #8232 |
On Jul 25, 11:28 pm, "Alf P. Steinbach"
<alf.p.steinbach:use...@gmail.com> wrote:
> On 26.07.2011 05:45, cerr wrote:
>
>
>
> > I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
> > $ g++ -o example example.cpp
> > example.cpp: In member function ‘int MyThread::StartMe()’:
> > example.cpp:42:59: error: expected primary-expression before ‘void’
> > example.cpp:42:63: error: initializer expression list treated as compound expression
> > the code:
> > int MyThread::StartMe(void)
> > {
> > int pthread_create(&ThreadA, NULL,&MyThread::PrintMsg, void);
>
> `void` is a type.
Hi Alf, yep I've figured that out by now but passing NULL doesn't work
either, how do I pass a void argument when my thread function is
declared like: void PrintMsg(void);?
Thanks!
[toc] | [prev] | [next] | [standalone]
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-25 23:53 -0700 |
| Message-ID | <d7dba757-ed45-4a21-b086-e4b62b363e1d@u6g2000prc.googlegroups.com> |
| In reply to | #8233 |
i have now something like this:
int MyThread::StartMe(void)
{
// starting thread
pthread_create(&ThreadA, NULL, &PrintMsg, NULL);
}
//-------------------------------------------------------------
void PrintMsg(void)
{
//thread function
while (1) {
pthread_mutex_lock( &mutex1 );
count++;
pthread_mutex_unlock( &mutex1 );
cout << "I Incremented count: " << count << endl;
}
}
but I get following:
$ g++ -pthread -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:66:49: error: invalid conversion from ‘void (*)()’ to
‘void* (*)(void*)’
example.cpp:66:49: error: initializing argument 3 of ‘int
pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*),
void*)’
How do I gwet this compiled? How do I pass a void argument?
Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-26 19:43 +1200 |
| Message-ID | <9979gdFkjuU14@mid.individual.net> |
| In reply to | #8235 |
On 07/26/11 06:53 PM, cerr wrote:
> i have now something like this:
> int MyThread::StartMe(void)
> {
> // starting thread
> pthread_create(&ThreadA, NULL,&PrintMsg, NULL);
>
> }
> //-------------------------------------------------------------
>
> void PrintMsg(void)
> {
> //thread function
> while (1) {
>
> pthread_mutex_lock(&mutex1 );
> count++;
> pthread_mutex_unlock(&mutex1 );
>
> cout<< "I Incremented count: "<< count<< endl;
> }
> }
> but I get following:
>
> $ g++ -pthread -o example example.cpp
> example.cpp: In member function ‘int MyThread::StartMe()’:
> example.cpp:66:49: error: invalid conversion from ‘void (*)()’ to
> ‘void* (*)(void*)’
> example.cpp:66:49: error: initializing argument 3 of ‘int
> pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*),
> void*)’
>
> How do I gwet this compiled? How do I pass a void argument?
> Thank you!
Pass the the correct function type!
extern "C" void* PrintMsg(void*)
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | "io_x" <a@b.c.invalid> |
|---|---|
| Date | 2011-07-26 09:57 +0200 |
| Message-ID | <4e2e730d$0$44198$4fafbaef@reader1.news.tin.it> |
| In reply to | #8235 |
"cerr" <ron.eggler@gmail.com> ha scritto nel messaggio
news:d7dba757-ed45-4a21-b086-e4b62b363e1d@u6g2000prc.googlegroups.com...
i have now something like this:
int MyThread::StartMe(void)
{
// starting thread
pthread_create(&ThreadA, NULL, &PrintMsg, NULL);
}
//-------------------------------------------------------------
void PrintMsg(void)
{
//thread function
while (1) {
pthread_mutex_lock( &mutex1 );
count++;
pthread_mutex_unlock( &mutex1 );
cout << "I Incremented count: " << count << endl;
}
}
but I get following:
$ g++ -pthread -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:66:49: error: invalid conversion from ‘void (*)()’ to
‘void* (*)(void*)’
example.cpp:66:49: error: initializing argument 3 of ‘int
pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*),
void*)’
How do I gwet this compiled? How do I pass a void argument?
Thank you!
-------------------
i modify somthing; in printmsg i impose count<100000
unsigned count=0;
void* PrintMsg(void* unused)
{(void) unused;
//thread function
while (1)
{ pthread_mutex_lock( &mutex1 );
count++;
pthread_mutex_unlock( &mutex1 );
cout << "I Incremented count: " << count << endl;
if(count>=100000) break;
}
return 0;
}
void MyThread::StartMe(void)
{// starting thread
pthread_create(&ThreadA, 0, &PrintMsg, 0);
}
[toc] | [prev] | [next] | [standalone]
| From | "io_x" <a@b.c.invalid> |
|---|---|
| Date | 2011-07-26 10:31 +0200 |
| Message-ID | <4e2e7ad0$0$15664$4fafbaef@reader2.news.tin.it> |
| In reply to | #8238 |
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4e2e730d$0$44198$4fafbaef@reader1.news.tin.it...
>
> "cerr" <ron.eggler@gmail.com> ha scritto nel messaggio
> news:d7dba757-ed45-4a21-b086-e4b62b363e1d@u6g2000prc.googlegroups.com...
> i have now something like this:
> int MyThread::StartMe(void)
> {
> // starting thread
> pthread_create(&ThreadA, NULL, &PrintMsg, NULL);
>
> }
> //-------------------------------------------------------------
>
> void PrintMsg(void)
> {
> //thread function
> while (1) {
>
> pthread_mutex_lock( &mutex1 );
> count++;
> pthread_mutex_unlock( &mutex1 );
>
> cout << "I Incremented count: " << count << endl;
> }
> }
> but I get following:
>
> $ g++ -pthread -o example example.cpp
> example.cpp: In member function ‘int MyThread::StartMe()’:
> example.cpp:66:49: error: invalid conversion from ‘void (*)()’ to
> ‘void* (*)(void*)’
> example.cpp:66:49: error: initializing argument 3 of ‘int
> pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*),
> void*)’
>
> How do I gwet this compiled? How do I pass a void argument?
> Thank you!
> -------------------
> i modify somthing; in printmsg i impose count<100000
>
> unsigned count=0;
>
> void* PrintMsg(void* unused)
> {(void) unused;
> //thread function
> while (1)
> { pthread_mutex_lock( &mutex1 );
> count++;
> pthread_mutex_unlock( &mutex1 );
>
> cout << "I Incremented count: " << count << endl;
> if(count>=100000) break;
> }
> return 0;
> }
>
> void MyThread::StartMe(void)
> {// starting thread
> pthread_create(&ThreadA, 0, &PrintMsg, 0);
> }
i forget that it is output that has need mutex;
but where are the headers i has to link with this?
unsigned count=0;
unsigned index=-1;
unsigned array[256];
void* PrintMsg(void* a)
{//thread function
unsigned aa;
aa=*(unsigned*)a
while (1)
{ pthread_mutex_lock( &mutex1 );
count++;
cout << "Tread number=" << aa << "; ";
cout << "I Incremented count: " << count << "\n";
pthread_mutex_unlock( &mutex1 );
if(count>=100000) break;
}
return 0;
}
void StartMe(int i)
{// starting thread
++index; index&=255; array[index]=i;
pthread_create(&ThreadA, 0, &PrintMsg, (void*)(array+index));
}
int main(void)
{int i;
for(i=0; i<100; ++i)
StartMe(i);
}
[toc] | [prev] | [next] | [standalone]
| From | "io_x" <a@b.c.invalid> |
|---|---|
| Date | 2011-07-26 12:38 +0200 |
| Message-ID | <4e2e98a0$0$44201$4fafbaef@reader1.news.tin.it> |
| In reply to | #8239 |
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4e2e7ad0$0$15664$4fafbaef@reader2.news.tin.it...
what do you think about this?
It is a good way for threads?
#include <stdio.h>
#include <iostream.h>
#include <windows.h>
unsigned count=0;
unsigned index=-1;
unsigned array[256];
#define ini InitializeCriticalSection
#define lock EnterCriticalSection
#define unlock LeaveCriticalSection
#define end DeleteCriticalSection
#define P printf
CRITICAL_SECTION dataTh;
int hhh;
unsigned long __stdcall PrintMsg(void* a)
{//thread function
unsigned aa;
aa=*(unsigned*)a;
while(count<=100)
{lock( &dataTh );
count++;
if(count<=100)
{cout << "Tread number=" << aa << "; ";
cout << "I Incremented count: " << count << "\n";
}
else {++hhh; P("wait 10sec hhh=%d\r", hhh);}
unlock( &dataTh );
}
return 0;
}
unsigned StartMe(int i)
{// starting thread
unsigned long id=0;
void *hThread;
++index; index&=255; array[index]=i;
hThread=CreateThread(0, 0, PrintMsg, array+index, 0, &id);
if(hThread == 0 ) return -1;
return 0;
}
int main(void)
{int i;
ini(&dataTh);
for(i=0; i<100; ++i)
if((int)StartMe(i)==-1) break;
Sleep(10000); // 10 secondi
end(&dataTh);
printf("\n");
return 0;
}
Tread number=0; I Incremented count: 1
Tread number=1; I Incremented count: 2
Tread number=3; I Incremented count: 3
Tread number=5; I Incremented count: 4
Tread number=2; I Incremented count: 5
Tread number=7; I Incremented count: 6
Tread number=9; I Incremented count: 7
Tread number=11; I Incremented count: 8
Tread number=13; I Incremented count: 9
Tread number=4; I Incremented count: 10
Tread number=6; I Incremented count: 11
Tread number=8; I Incremented count: 12
Tread number=15; I Incremented count: 13
Tread number=10; I Incremented count: 14
Tread number=12; I Incremented count: 15
Tread number=17; I Incremented count: 16
Tread number=19; I Incremented count: 17
Tread number=21; I Incremented count: 18
Tread number=14; I Incremented count: 19
Tread number=16; I Incremented count: 20
Tread number=0; I Incremented count: 21
Tread number=24; I Incremented count: 22
Tread number=23; I Incremented count: 23
Tread number=25; I Incremented count: 24
Tread number=27; I Incremented count: 25
Tread number=29; I Incremented count: 26
Tread number=22; I Incremented count: 27
Tread number=20; I Incremented count: 28
Tread number=18; I Incremented count: 29
Tread number=31; I Incremented count: 30
Tread number=26; I Incremented count: 31
Tread number=33; I Incremented count: 32
Tread number=28; I Incremented count: 33
Tread number=35; I Incremented count: 34
Tread number=30; I Incremented count: 35
Tread number=37; I Incremented count: 36
Tread number=39; I Incremented count: 37
Tread number=1; I Incremented count: 38
Tread number=41; I Incremented count: 39
Tread number=43; I Incremented count: 40
Tread number=45; I Incremented count: 41
Tread number=32; I Incremented count: 42
Tread number=34; I Incremented count: 43
Tread number=36; I Incremented count: 44
Tread number=47; I Incremented count: 45
Tread number=49; I Incremented count: 46
Tread number=38; I Incremented count: 47
Tread number=40; I Incremented count: 48
Tread number=51; I Incremented count: 49
Tread number=53; I Incremented count: 50
Tread number=42; I Incremented count: 51
Tread number=55; I Incremented count: 52
Tread number=44; I Incremented count: 53
Tread number=59; I Incremented count: 54
Tread number=57; I Incremented count: 55
Tread number=46; I Incremented count: 56
Tread number=48; I Incremented count: 57
Tread number=63; I Incremented count: 58
Tread number=61; I Incremented count: 59
Tread number=50; I Incremented count: 60
Tread number=3; I Incremented count: 61
Tread number=65; I Incremented count: 62
Tread number=66; I Incremented count: 63
Tread number=67; I Incremented count: 64
Tread number=64; I Incremented count: 65
Tread number=69; I Incremented count: 66
Tread number=62; I Incremented count: 67
Tread number=60; I Incremented count: 68
Tread number=58; I Incremented count: 69
Tread number=56; I Incremented count: 70
Tread number=54; I Incremented count: 71
Tread number=52; I Incremented count: 72
Tread number=71; I Incremented count: 73
Tread number=73; I Incremented count: 74
Tread number=75; I Incremented count: 75
Tread number=68; I Incremented count: 76
Tread number=77; I Incremented count: 77
Tread number=79; I Incremented count: 78
Tread number=70; I Incremented count: 79
Tread number=72; I Incremented count: 80
Tread number=81; I Incremented count: 81
Tread number=76; I Incremented count: 82
Tread number=5; I Incremented count: 83
Tread number=74; I Incremented count: 84
Tread number=83; I Incremented count: 85
Tread number=87; I Incremented count: 86
Tread number=89; I Incremented count: 87
Tread number=91; I Incremented count: 88
Tread number=93; I Incremented count: 89
Tread number=95; I Incremented count: 90
Tread number=85; I Incremented count: 91
Tread number=97; I Incremented count: 92
Tread number=78; I Incremented count: 93
Tread number=99; I Incremented count: 94
Tread number=80; I Incremented count: 95
Tread number=82; I Incremented count: 96
Tread number=84; I Incremented count: 97
Tread number=86; I Incremented count: 98
Tread number=88; I Incremented count: 99
Tread number=90; I Incremented count: 100
wait 10sec hhh=100
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-26 22:39 +1200 |
| Message-ID | <997jqqFkjuU17@mid.individual.net> |
| In reply to | #8243 |
On 07/26/11 10:38 PM, io_x wrote: > "io_x"<a@b.c.invalid> ha scritto nel messaggio > news:4e2e7ad0$0$15664$4fafbaef@reader2.news.tin.it... > > what do you think about this? > It is a good way for threads? > > #include<stdio.h> > #include<iostream.h> > #include<windows.h> > > unsigned count=0; > unsigned index=-1; > unsigned array[256]; > > #define ini InitializeCriticalSection NO! -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-26 22:41 +1200 |
| Message-ID | <997juhFkjuU18@mid.individual.net> |
| In reply to | #8238 |
On 07/26/11 07:57 PM, io_x wrote:
> -------------------
> i modify somthing; in printmsg i impose count<100000
>
> unsigned count=0;
>
> void* PrintMsg(void* unused)
A C library expects a C linkage function pointer, so give it one.
> {(void) unused;
You don't need the awful Cism in C++, just leave the parameter unnamed.
--
Ian Collins
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web