Problem with pointers, structures, and Pthread


 
Thread Tools Search this Thread
Top Forums Programming Problem with pointers, structures, and Pthread
# 1  
Old 05-23-2007
Problem with pointers, structures, and Pthread

Hello all,

I'm working on a small wrapper library for a bigger project, and i've been killing my self over (what I think is) a pointer problem.

Here is the code (I extracted the part of the code where the problem is for better reading, I tested the code below, and I get the same problem):
wrapper.c
Code:
#include <stdio.h>
#include <pthread.h>
#include "rwmutex.h"

rwmutex_t* create_rwmutex()
{
	rwmutex_t m;
	
	pthread_mutex_init(&m.rm, NULL);
	pthread_mutex_init(&m.wm, NULL);
	pthread_cond_init(&m.rc, NULL);

	return &m;
}

void write_lock(rwmutex_t *m)
{
	pthread_mutex_lock(&m->wm);
}

void write_unlock(rwmutex_t *m)
{
	pthread_mutex_unlock(&m->wm);
}

wrapper.h
Code:
typedef struct
{
	pthread_mutex_t wm;		//write mutex
	pthread_mutex_t rm;		//read mutex
	pthread_cond_t rc;		//read condition var

} rwmutex_t;

rwmutex_t* create_rwmutex ();
void write_lock (rwmutex_t *m);
void write_unlock (rwmutex_t *m);

tester.c
Code:
#include <stdio.h>
#include <pthread.h>
#include "rwmutex.h"

void* f1(void* p);

rwmutex_t *m;
pthread_t t1, t2;

int main(int argc, char* argv[])
{
	m = create_rwmutex();

	pthread_create(&t1, NULL, f1, 4);
	sleep(1);
	pthread_create(&t2, NULL, f1, 5);
	
	pthread_join(t1, NULL);
	pthread_join(t2, NULL);

	return 0;
}

void* f1(void* p)
{
	int i = (int)p;
	write_lock(m);
	printf("[%d] WRITE IN\n", i);
	sleep(2);
	printf("[%d] WRITE OUT\n", i);
	write_unlock(m);
}

After compiling with 'gcc wrapper.c tester.c -o t -lpthread' .. I get some warnings.
When I execute './t' I get the output:
Code:
[4] WRITE IN
[5] WRITE IN
[4] WRITE OUT
[5] WRITE OUT

What I should get is a synchronized output, that is, the second thread waits for the first thread to complete, so the output should be:
Code:
[4] WRITE IN
[4] WRITE OUT
[5] WRITE IN
[5] WRITE OUT

I'm running this on a Windows environment using Cygwin.

Thanks in advance for your time,
Naimi
# 2  
Old 05-23-2007
Code:
#include <stdio.h>
#include <pthread.h>
#include "rwmutex.h"

rwmutex_t* create_rwmutex()
{
	rwmutex_t m;
	
	pthread_mutex_init(&m.rm, NULL);
	pthread_mutex_init(&m.wm, NULL);
	pthread_cond_init(&m.rc, NULL);

	return &m;
}

You shot yourself in the foot right at the start. You can't return a pointer to memory on the stack and expect it to be valid when it goes out of scope.

You either need to make it static or use malloc.
# 3  
Old 05-23-2007
Thank you Porter,
I changed my code as you said to
Code:
rwmutex_t* create_rwmutex()
{
	rwmutex_t *m;
	m = (rwmutex_t*)malloc(sizeof(rwmutex_t));
	
	pthread_mutex_init(&m->rm, NULL);
	pthread_mutex_init(&m->wm, NULL);
	pthread_cond_init(&m->rc, NULL);

	return m;
}

Now it is working fine.
Thanks again.
Cheers
# 4  
Old 12-19-2007
pls brother, may i get a copy of this code - Read/Write Locks with Quota code with its methodes bcoz i need it in my studies Smilie
# 5  
Old 12-19-2007
Quote:
Originally Posted by just student
bcoz i need it in my studies Smilie
FYI: Rule 6, no homework.
# 6  
Old 12-19-2007
Quote:
Originally Posted by porter
FYI: Rule 6, no homework.
just some mercy mr porterSmilie
# 7  
Old 12-19-2007
... but how will you learn?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Signalsafe data structures

Hello, I have a signal handler which manipulates a data structure. The data structure's operations aren't atomic. So if two threads/processes are in a critical section at the same time the data structure will be broken. With threads you can avoid this stuff with semaphores etc. However,... (10 Replies)
Discussion started by: littlegnome
10 Replies

2. Programming

Passing Pointers by reference in C++ Problem

Hello All, I am having this issue...where I am actually having hard time understanding the problem: The code is as follows: #include<iostream.h> void fxn(char*** var) { int i =4; *var = (char**)malloc(i*sizeof(char*)); for(int j =0; j<4; j++) { *var = "name"; cout<<*var;... (6 Replies)
Discussion started by: mind@work
6 Replies

3. Programming

Problem With Pointers

Hi guys. What is the difference between these: 1. int *a; 2. int (*a); (2 Replies)
Discussion started by: majid.merkava
2 Replies

4. Ubuntu

pthread problem

Hi all, I wrote some code in c, using pthread (I configured the linker and compiler in eclipse IDE first). #include <pthread.h> #include "starter.h" #include "UI.h" Page* MM; Page* Disk; PCB* all_pcb_array; void* display_prompt(void *id){ printf("Hello111\n"); return... (1 Reply)
Discussion started by: elad2109
1 Replies

5. Programming

Problem with array of pointers

Hi All, I am using the array of pointers and storing the address of string.This is a global list. So i am using extern to give the reference of this list to another file and using reading the data from this string. But list is being corrupted and string is missing some characters in... (2 Replies)
Discussion started by: lovevijay03
2 Replies

6. UNIX for Dummies Questions & Answers

a pthread problem

Hello, I run my pthread code on Linux with 4 processors. However, the speed up is only 2 times. The code is about solving equation (G+s(i)C)z(i)=B*us(i), i=1,...,n. Here G,C are m*m matrix, B*us(i) is a m*1 vector and s(i) are n different numbers. I need to solve the equation n times to... (2 Replies)
Discussion started by: mgig
2 Replies

7. Linux

about system structures

hello can any1 plz tell me about the system defined structures (like sysinfo) which wil give system and n/w charecteristics (ex: freeram in sysinfo). (1 Reply)
Discussion started by: jeenat
1 Replies

8. Solaris

pthread problem

Hi all! I am working on unix systems.I am programming in c. I have got some problems with pthread.when I use pthread_create to creat a thread it says: (.text+0x3a): undefined reference to `pthread_create'. same is the problm with pthread_kill. Can anyone help me out here. Thanks. vij. (2 Replies)
Discussion started by: vijlak
2 Replies

9. Programming

Programming using Structures

Hi All, I was given a format of a file, and was asked to write a program which displays the data contained in the file in that purticular format. Its all so confusing. Please find the example of the format as well the code I have written in the attachment. I hope any one of u guyz can... (0 Replies)
Discussion started by: jazz
0 Replies

10. Programming

pointer to structures

Dear friends I have a bit basic doubts in pointers and the structures inter relationships. the first one. static struct apvt { int dead; int pending; int abouttograb; }*agents=NULL; what agents pointer is... (1 Reply)
Discussion started by: tech_voip
1 Replies
Login or Register to Ask a Question