Thread question
- From: "Jack" <junw2000@xxxxxxxxx>
- Date: 24 Jan 2007 09:59:08 -0800
Hi,
I create two threads. Both of them will execute the same function as
below:
pthread_t thread1, thread2;
void
printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
void *
thr_fn(void *arg)
{
printids("new thread: ");
return((void *)0);
}
int
main(void)
{
int err1, err2;
err1 = pthread_create(&thread1, NULL, thr_fn, NULL);
err2 = pthread_create(&thread2, NULL, thr_fn, NULL);
.......
}
Since the two threads execute the same function. They share the same
variables, right?
Will they conflict with each other? Should I use mutex or semaphore?
The above code is a simple example. Generally, if two threads execute
the same function, should mutex be used to prevent conflicting?
Thanks.
Jack
.
- Follow-Ups:
- Re: Thread question
- From: Loic Domaigne
- Re: Thread question
- From: Mihai Osian
- Re: Thread question
- Prev by Date: Re: File locking in Linux
- Next by Date: Re: File locking in Linux
- Previous by thread: attn: wadsworth - very gratifying spam free nntp access - rovli uc - (1/1)
- Next by thread: Re: Thread question
- Index(es):
Relevant Pages
|