pthread mutex not working

Hi,
I have created a C programusing pthread.
[Code]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// A normal C function that is executed as a thread
// when its name is specified in pthread_create()

pthread_mutex_t lock;

void *myThreadFun1(void *vargp)
{

pthread_mutex_lock(&lock);
int i=0;
for(i=0;i<200;i++)
printf ("i=%d",i);
pthread_mutex_unlock(&lock);


}
void *myThreadFun2(void *vargp){
int j=0;
for(j=0; j<200; j++)
printf("j=%d", j);
}


int main()
{
pthread_t thread_id;
printf("Before Thread\n");
if(pthread_mutex_init(&lock, NULL) !=0){
printf("Mutex failed");
return -1;
}
pthread_create(&thread_id, NULL, myThreadFun1, NULL);
pthread_create(&thread_id, NULL, myThreadFun2, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
pthread_join(thread_id, NULL);
pthread_join(thread_id,NULL);
pthread_mutex_destroy(&lock);
}
[/code]

I have created two threads. Each thread is supposed to run a different function. In my myThreadFun1, I have created a lock so that its output should not interleave with the output of myThreadFun2. But the program is displaying following interleaved output:

$ ./a.out
Before Thread
i=0i=1i=2i=3i=4i=5i=6i=7i=8i=9i=10i=11i=12i=13i=14i=15i=16i=17i=18i=19i=20i=21i=22i=23i=24i=25i=26i=27i=28i=29i=30i=31i=32i=33i=34i=35i=36i=37i=38i=39i=40i=41i=42i=43i=44i=45i=46i=47i=48i=49i=50i=51i=52i=53i=54i=55i=56i=57i=58i=59i=60i=61i=62i=63i=64i=65i=66i=67i=68i=69i=70i=71i=72i=73i=74i=75i=76i=77i=78i=79j=0i=80j=1j=2i=81j=3i=82j=4i=83j=5i=84i=85i=86i=87j=6i=88j=7i=89j=8i=90j=9i=91j=10i=92j=11j=12j=13i=93j=14i=94j=15i=95j=16i=96j=17i=97j=18i=98i=99i=100j=19i=101j=20i=102i=103i=104i=105j=21i=106j=22j=23i=107j=24i=108i=109i=110i=111j=25i=112j=26i=113j=27i=114j=28i=115j=29i=116j=30j=31i=117i=118j=32i=119j=33i=120j=34i=121i=122i=123i=124j=35i=125i=126j=36i=127j=37i=128i=129i=130i=131i=132j=38i=133j=39i=134j=40i=135i=136i=137i=138j=41i=139j=42i=140j=43i=141j=44i=142i=143i=144i=145i=146j=45i=147j=46j=47i=148j=48i=149j=49i=150j=50i=151i=152i=153i=154j=51i=155j=52i=156j=53i=157j=54j=55i=158j=56i=159j=57j=58i=160j=59i=161j=60i=162i=163i=164i=165i=166j=61i=167j=62i=168j=63i=169i=170i=171i=172i=173j=64i=174j=65i=175i=176i=177i=178i=179i=180i=181i=182i=183i=184i=185i=186i=187i=188i=189i=190i=191i=192i=193i=194i=195i=196i=197i=198i=199j=66j=67j=68j=69j=70j=71j=72j=73j=74j=75j=76j=77j=78j=79j=80j=81j=82j=83j=84j=85j=86j=87j=88j=89j=90j=91j=92j=93j=94j=95j=96j=97j=98j=99j=100j=101j=102j=103j=104j=105j=106j=107j=108j=109j=110j=111j=112j=113j=114j=115j=116j=117j=118j=119j=120j=121j=122j=123j=124j=125j=126j=127j=128j=129j=130j=131j=132j=133j=134j=135j=136j=137j=138j=139j=140j=141j=142j=143j=144j=145j=146j=147j=148j=149j=150j=151j=152j=153j=154j=155j=156j=157j=158j=159j=160j=161j=162j=163j=164j=165j=166j=167j=168j=169j=170j=171j=172j=173j=174j=175j=176j=177j=178j=179j=180j=181j=182j=183j=184j=185j=186j=187j=188j=189j=190j=191j=192j=193j=194j=195j=196j=197j=198j=199After Thread





Somebody please guide me how to solv this problem.

Zulfi.
Last edited on
1. It's [code][/code], not <code></code> (despite the misleading shape on the icons).

2. You NEED the mutex in myThreadFun2 as well.

Hi,
Thanks.

It worked.

God bless you.

Zulfi.
Hi your code is not correct which is define below:

pthread_create(&thread_id, NULL, myThreadFun1, NULL);


pthread_mutex_t lock;

void *myThreadFun1(void *vargp)
{

pthread_mutex_lock(&lock);
int i=0;
for(i=0;i<200;i++)
printf ("i=%d",i);
pthread_mutex_unlock(&lock);


}
void *myThreadFun2(void *vargp){
int j=0;
for(j=0; j<200; j++)
printf("j=%d", j);
}


int main()
{
pthread_t thread_id;
printf("Before Thread\n");
if(pthread_mutex_init(&lock, NULL) !=0){
printf("Mutex failed");
return -1;
}
pthread_create(&thread_id, NULL, myThreadFun1, NULL);
pthread_create(&thread_id, NULL, myThreadFun2, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
pthread_join(thread_id, NULL);
pthread_join(thread_id,NULL);
pthread_mutex_destroy(&lock);
}
[/code]

for more click this link:- https://www.cetpainfotech.com/
Registered users can post here. Sign in or register to post.