C++ :: Creating And Terminating Threads?
Aug 31, 2014
I had a requirement where i needed to create a thread and if the execution of thread is not completed in 5 minutes i needed to terminate its execution and continue with other part of the code.
I used the below code to create the thread
_beginthread(FuncnCall,0,NULL);
HANDLE hThread = GetCurrentThread();
Then after this code, I used the below code to check for 5 minutes
for (int i=1;i<=0;i++) {
printf("Value of i=%d
",i);
if(threadFinished) {
break;
} else {
Sleep(1000);
}
}
After this if the value of "threadFinished" is false then i am terminating the thread like below
if(threadFinished == false) {
TerminateThread(hThread,0);
CloseHandle(hThread);
}
The Problem here is, after terminating the thread, the program abruptly closes by giving fatal error. Looks like memory leakage is happening after terminating the thread. Is it not the right way to safely exit the thread?
View 2 Replies
ADVERTISEMENT
Jan 26, 2014
I am creating a BST class that has insertion and traversal function (USING RECURSION)... why this program is termination abnormally..
Code:
#include <iostream>#include <conio.h>
using namespace std;
class Node//BST node {
friend class BST;
private:
[Code].....
View 7 Replies
View Related
Dec 26, 2014
I know that the null at the end of the string indicates end of that string but why is it actually needed. Strings in C are just arrays of char variables. A "Hello" string would be stored in ascii code as:
char string[] = {72, 101, 108, 108, 111, 0}
When I have array of integers like
int intArray[] = {72, 101, 108, 108, 111}
I can perform various operations with it like comparision with other array etc. but I don't need terminating character
View 6 Replies
View Related
Feb 3, 2014
I have the following code
#include "../header/FileIO.hpp"
#include <fstream>
#include <stdexcept>
#include <iostream>
std::string FileIO::ReadTXT(std::string filePath){
std::ifstream input(filePath.c_str());
[Code] ....
When I run it in Eclipse, it does not open an SDL window, and simply says <Terminated> Test.exe [C/C++ Application]. I can build the project successfully, but it simply won't run. There are no errors displayed.
HOWEVER, if I replace the above code with the code below, it runs fine, and creates an SDL window.
#include "../header/FileIO.hpp"
#include <fstream>
#include <stdexcept>
#include <iostream>
std::string FileIO::ReadTXT(std::string filePath) {
std::string output;
return output;
}
To make things weirder, if I run it in debug mode, even with the first piece of code, it will run and open an SDL window.
On an unrelated note, what does " Can't find a source file at "e:pgiawsrcpkgmingwrt-4.0.3-1-mingw32-srcld/../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c"
Locate the file or edit the source lookup path to include its location." mean? I am using Windows 8, MinGW, GDB 7.6.1-1, G++ 4.8.1-4, and MSYS 1.0.1 with Eclipse CDT.
View 3 Replies
View Related
Oct 6, 2014
I am suppose to make a program that when the user is asked "Enter a Letter for the day:" if the user enters M or m then the screen will output "The day of the week is Monday" and so on until Sunday. I looked over my code and everything looks right but I still get errors saying Missing terminating character " and int function main error.
#include <iostream>
using namespace std;
int main() {
char day;
cout << "Enter a letter for a day of the week: ";
[code]....
View 1 Replies
View Related
Jul 23, 2014
I wrote a code to read a specific amount of data from file until terminating set is reached. however the code does what its supposed to correctly until it reaches the 5th loop it justs stops responding. I've checked the reading file and all elements are correct in the file.
Code:
int main(void){
FILE *file1;
FILE *file2;
FILE *file3;
struct t test1;
struct t test2;
[Code] ....
Screenshot of program crashing:
Screenshot of reading file:
View 11 Replies
View Related
Nov 11, 2013
I've to use SIGNALS to control threads. Here's my code without the use of signals as I'm no good at that
Code:
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
}
[code]....
View 4 Replies
View Related
Jun 30, 2014
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int turn = 1;
int counter = 0;
int t, n;
[Code] ....
I found this program in a forum and it creates 1 to t threads with each thread pointing to the next and the last thread pointing to the first thread and it allows each thread to sequentially take a turn until all threads have taken n turns. That is when the program ends.
My doubt is if we see in void *tFunc(void *arg) function, first thread locks the mutex and then wait there forever till it's turn comes.Lets say if I spwan 1000 threads and they have take 10 turns each sequentially and what if a 550th thread locks the mutex as soon as the program starts and waiting for its turn which will never happen because still (turn=1) which means it's first thread's turn and first 549 threads have to complete their 1st turn before this 550 threda's turn comes.Doesnt this result in deadlock?
View 2 Replies
View Related
Apr 29, 2014
how to encapsulate two threads in a class?
View 1 Replies
View Related
Aug 31, 2014
Here's my code,
#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
void Blink();
[Code] .....
The problem is i don't want to use local vars , is ther anyway do end the loop safely(removing all the vars used in function and etc) without using a local var ?
View 3 Replies
View Related
Apr 18, 2014
#include <stdio.h>
#include <pthread.h>
int i; // loop control variable
int *id; // array used to pass parameter of id
int nThreads; // #threads
[Code]....
I want to know where I am going wrong. I am strong in java but not in c so that may be my problem.
View 9 Replies
View Related
Feb 6, 2014
Is there a way of knowing which indices a thread is assigned in a parallel openMP scope?
View 6 Replies
View Related
Jun 2, 2014
I am on a little project involving TCP socket-programming and multiple threads but need passing structures between two threads.
The first thread receives data with respective ID, temp, etc,.. Each ID is a list-item of a linked list and every ID has again a linked list of all the received data.
The second thread just have to access the list and perform some manipulations on it.
My solution: pass the head pointer of the linked list. Easy enough, right? Somehow, I am stuck receiving error message:" dereferencing pointer to incomplete type."
For the sake of ease and simplicity I just added a stripped down version of the project.
The magic (or not) happens in MainProcess.c: The Data thread should receive the pointer (a think) from the Connection thread.
View 5 Replies
View Related
May 9, 2013
I am doing something wrong since for me 4 threads perform 2 times slower then 1.I have 2 vectors with bunch of data to process, there is no concurrency (not moving elements and are independent of each other) so i just need to calculate some data from one and copy result in another.
LARGE_INTEGER getFrequency()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return frequency;
[code].....
View 7 Replies
View Related
Mar 1, 2013
I want to create two threads which will be calling even and odd functions where even function should print even number and odd function should print odd number.Can it be possible with condition variable? What is the code in both the cases i.e. two separate function and with condition variable.
View 6 Replies
View Related
Jul 9, 2012
how do I stop and restart a Thread in C# once it reaches a certain location on a form then restart the thread once it goes outside of the location boundries?
I am trying to stop an animation thread (a bullet) once it reaches a block on a panel.
View 1 Replies
View Related
Jul 25, 2014
Code:
void Arrival_MainThread::Body () {
t1 = new boost::thread((Arrive_Fctor()), 2);
t2 = new boost::thread((Arrive_Fctor()), 10000);
t3 = new boost::thread((Arrive_Fctor()), 3000);
Code:
class Arrive_Fctor {
public:
void operator()(int mean) {
m_Arrivals = new Arrivals(mean);
m_Arrivals->Activate();
#ifndef NO_RESOURCE
[code].....
The order of arrivals always appear as 3,2,1 it can't be 1,2,3 or 3,1,2 or something like that?
The body method is called when m_Arrival->Activate() is executed, and it is running as an independent thread.
View 4 Replies
View Related
Aug 27, 2014
I write a DLL MyDLL.dll with Visual C++ 2008, as follows:
(1)MFC static linked
(2)Using multi-thread runtime library.
In the DLL, this is a global data m_Data shared by two export functions, as follows:
ULONGLONG WINAPI MyFun1(LPVOID *lpCallbackFun1) {
...
Write m_Data(using Critical section to protect)
[Code]....
Although MyThread1 and MyThread2 using critical section to protect the shared data m_Data, I will still suspend MyThread1 before accessing the shared data, to prevent any possible conflicts.
The problem is:
(1)When the first invoke of MyFun2, everything is OK, and the return value of MyFun2(that is nResult2) is 1 , which is expected.
(2)When the second, third and fourth invoke of MyFun2, the operations in MyFun2 are executed successfully, but the return value of MyFun2(that is nResult2) is a random value instead of the expected value 1. I try to using Debug to trace into MyFun2, and confirm that the last return statement is just return a value of 1, but the invoker will receive a random value instead of 1 when inspecting nResult2.
(3)After the fourth invoke of MyFun2 and return back to the next statement follow MyFun2, I will always get a "buffer overrun detected" error, whatever the next statement is.
I think this looks like a stack corruption, so try to make some tests:
1.I confirm the /GS (Stack security check) feature in the compiler is ON.
2.If MyFun2 is invoked after MyFun1 in MyThread1 is completed, then everything will be OK.
3.In debug mode, the codeline in MyFun2 that reads the shared data m_Data will not cause any errors or exceptions. Neither will the codeline in MyFun1 that writes the shared Data.
View 4 Replies
View Related
Oct 20, 2014
I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.
The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.
However, if I need to switch from vectors to something else then I am open to that as well.
The following example below compiles but crashes rather quickly because it is not thread safe.
How I can fix the example below which I can then apply to my larger project?
#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {
[Code] ....
View 1 Replies
View Related
Dec 14, 2014
I am trying to write a code that solves a system of linear equations such as A*B=C. My system has a dimension equal to 1600. The matrix A cab be separated into 4 sub matrices and each can be handled by a different thread. I tried to solve this using the following code:
int main() {
int count = 0;
//Inputing matrix A
ifstream matrix;
matrix.open("example.txt");
[Code] ....
Although the above code gives the correct answer, the time needs to find the solution is bigger than that needed without using threads.
View 1 Replies
View Related
Aug 7, 2013
I'm currently working on a server for handling clients in a 2d online game and I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently, so I googled it and came up with
posts like
[URL]
I'm wondering if I can just treat it like everything else or will I have to do something specific for opening on multiple threads?
p.s. I did read those posts but I'm very new to multithreading
View 16 Replies
View Related
Nov 6, 2013
I have a main thread and a worker thread. How do i pass data between them when both are already running without using global variables?
View 1 Replies
View Related
Dec 3, 2013
I have a school assignment to create a program that uses a producer thread to create a series of random numbers, and a consumer thread to read those numbers (via a shared bounded buffer), and record the min, max, average, and number of items. Now, the threads aren't synching well. Here's my code:
Code:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
pthread_cond_t empty;
pthread_cond_t full;
int done = 0;
pthread_mutex_t lock;
[code] ....
And output:
Code:
Producer in for
Consumer Entering While
Consumer reading item 0
Producer making item 0
Consumer reading item 1
Consumer reading item 2
Consumer reading item 3
Producer making item 1
Producer making item 2
Producer making item 3
Producer making item 4
Producer making item 5
Producer making item 6
Consumer reading item 4
Minimum: 0
Maximum: 4978
Average: 995
Items Produced: 5
View 4 Replies
View Related
Sep 18, 2013
I'm doing work on chat messenger and facing difficulty to run multiple threads at the same time. Here is the sample code.
void menu();
void groupChat();
void personalChat();
int main() {
sf::Thread thread(&menu());
thread.launch();
[Code] ....
In my program, after menu when he selects a choice, next display of menu wait the termination of the selected thread.
while i want to show menu right after when a menu is selected.
View 2 Replies
View Related
Jan 27, 2015
I made a simple binary tree then decide to try out threads too. I got the following error:
call of an object of a class type without appropriate operator or conversion
Code:
#include "Tree.h"
#include <iostream>
#include <thread>
void main(void){
[Code] ....
I am having a hard time figuring out why the error exists. I tried adding the new operator but that did not work.
View 11 Replies
View Related
May 11, 2013
How do you create a new directory in C++? I would like to do this using the standard library and no external, third party or non standard libraries/headers. I need to do this without calling system() or system("mkdir").
View 7 Replies
View Related