Multi-Threading in Cpp

Hi Members,

I am trying to learn multi-threading .
Following code is for a linked list using multi-thread in cpp.

Since std::thread needs a "pass by value",I have used the ref(),to get it fixed,

unfortunately I am getting " expected primary-expression before ‘)’" error."

I am unable to find it out ,what I am missing ,
please help me out.

Thanks in advance.

1
2
3
4
5
6
7
struct Node{
 int _minfo;
 Node* _mptr;
/* "Normal Constructor"*/
 Node(int data =0 ,Node* ptr = NULL ):_minfo(data),_mptr(ptr){}
}; 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
void insertNode(struct Node* head){
	
	Node *newNode = new Node;
	int info,data,pos = 0;
	cout<<"Enter the data to be added \n";
	cin>>info;
	newNode->_minfo = info;
	cout<<"Press 1 to add at head \n";
	cout<<"Press 2 for other position\n";
	cin>>pos;
	cout<<endl;
	if (pos == 1){
		newNode->_mptr = head->_mptr;
		head->_mptr = newNode;
	}
	else{
		Node *curNode = head;
		cout<<"Enter data after which you want to add\n";
		cin>>data;
		while( (curNode->_minfo != data )&&(curNode->_mptr != NULL ) ){
			curNode = curNode->_mptr;
		}
		if(curNode == NULL){
			cout<<"Error\n";
		}
		newNode->_mptr = curNode->_mptr;
		curNode->_mptr = newNode;
	}
}
void showList(struct Node* head){
	
	Node* curNode= head->_mptr;
	while (curNode!= NULL){
		cout<<curNode->_minfo<<endl;
		curNode= curNode->_mptr;
	}
}
void deleteNode (struct Node* head){
	int num=0;
	cout<<"Enter The Node to be Deleted\n";
	cin>>num;
	if(head->_mptr== NULL){
		cout<<"Head is NULL .. Exiting \n";
		exit(1);
	}
	Node* prevNode = head;
	Node *curNode = new Node;
	curNode = head;
	while((curNode->_minfo != num)&& (curNode->_mptr!= NULL)){
		prevNode = curNode;
		curNode = curNode->_mptr;
	}
	prevNode->_mptr = curNode->_mptr;
	delete (curNode);
}
int main(){

	cout<<"Creating Link List\n";
	int choice = 0;
	
	Node *head = new Node;
	
	void (*threadPtrInsrt)(struct Node*) = insertNode;
	void (*threadPtrShow)(struct Node*) = showList;
	void (*threadPtrDelete)(struct Node*) = deleteNode;
	
	while(1){
		cout<<"1:: INSERT\t 2:: SHOW\t 3:: DELETE\t 4:: Exit \n";
		cin>>choice;
		cout<<endl;
		switch(choice){
			case 1:{
				thread insertThread(threadPtrInsrt,ref(Node));
			}
			break;
			case 2:{
				thread showThread(threadPtrShow,ref(Node));
			}
			break;
			case 3:{
				thread delThread(threadPtrDelete,ref(Node));
			}
			break;
			case 4 :
			default:
				cout<<"Process Terminated \n";
				exit(1);
			break;
		}
	}
	return 0;
}


IT SEEMS THAT I CANNOT FORMAT THE CODE ,I TRIED USING THE "<>" BUTTON./
ITS NON-RESPONSIVE IN MY CASE .

SINCERE APOLOGIES FOR THE SAME.
Last edited on
thread insertThread(threadPtrInsrt,ref(Node));

This function expects you to pass it a pointer-to-a-Node.
void (*threadPtrInsrt)(struct Node*) = insertNode;

Are you passing it a pointer-to-a-Node? No.
To format code you merely write tags (when that button doesn't work), like this:

[code]
//your code goes here
[/code]


Further, the button works primarily when the code is selected.

Pressing that button merely inserts code tags, which you can type as exampled above.


Further, if you've already written several multi-threaded "study level" examples, this may be reasonable.

If this is one of your first few projects, it may be a very bad example to study threading.
Last edited on
@ Niccolo
It is in fact my first multi-threaded program.
I tried reading stuffs ,but it dint stuck me ,and the best comment I ever read was, "try implementing your basic programs with threads".

now since you have already termed it a bad example ,I would request you to provide with good "study level " examples.

and thanks for the tags :)
Last edited on
Start with something like:

Form an vector of structures, where the structure contains two operands (doubles, integers, whatever you like), and a compatible 'result' member of appropriate type. Create several thousand with random values as the two operands (say, A and B).

Now, loop through the vector to call a member function which calculates a result from A and B, say R = A * B. The detail doesn't matter.

Make sure that works, and you can see the results.

Then, re-work the code to launch a few threads (2, 3 or matching your core count). Have each thread handle a portion of the vector for the calculation, so they run in parallel. For example, if you have 10,000 structures, and launch 4 threads, have each thread process 2500 of the structures (but not overlapping, first one does 0 to 24999, second does 25000 to 49999, etc).

Join the threads with the main (this synchronizes the main to continue after these 4 exit).

See that the results are the same.

Then, look into std::async (and the associated future) and see if you can alter the code to use that library for the same basic threaded design.

Another example is a serial number.

Fashion a global integer, set it to an arbitrary start point, and use it to "dispense" id numbers. In single threaded mode, test to make sure you can generate serial ID numbers and store them in a vector.

Then, expand that to issue serial ID numbers in several threads at once. If the global integer isn't protected, there will be duplicate ID's issued. Find a way to discover them (or prove there are none).

Then, look into both mutex and lock, and practice "protecting" the global integer so only 1 thread can issue and ID number at a time. See if you can eliminate any duplicates.

Finally, change from using a mutex and a lock and instead use a std::atomic<int> (a lock free approach that doesn't need a mutex and a lock).


BTW, the reason I said the example you chose would be "bad" is that linked lists are by nature not thread friendly containers. Most containers aren't friendly to threads, and generally one must ensure only one thread operates the container at a time.

One might be able to sequence through and "read-only" from a linked list in multiple threads, but not deleted from it or append to it. It becomes rather tricky to allow reading in multiple threads while restricting writing.


Last edited on
@ Repeater

In fact ,I am not very used to function -pointer either.

What I intend to do with the code is :

create a function pointer for the
void insertNode(struct Node* head)
function ,since it receives a
struct Node* head type of argument

I defined my function pointer as
void (*threadPtrInsrt)(struct Node*) = insertNode; .

To be very honest I dint get your hint .

On a second thought are you asking me to put it something like this :

void (*threadPtrInsrt)(struct Node*) = insertNode(struct node*); .

(am without my system,cannot verify the above understanding. )

It would be really nice of you ,if you can explain it in details.
@ Niccolo ,Thanks
@ Repeater ,

I understand that the code is far from being good ,
but I got it figured what the issue was ,

thread insertThread(threadPtrDelete,ref(Node));

i am passing the Node ,which is meaningless ,instead

thread insertThread(threadPtrDelete,ref(head));

makes the code to compile.
Topic archived. No new replies allowed.