C/C++ :: Threading / How To Invoke It Properly
Jun 12, 2012
I'm using MS' optimizing compiler CL 13.10 (one from VCToolkit 2003) along with WinAPI threading functions and is being compiled as a C console program.
I was wondering how to implement threading in a production setting? I've seen and tried various examples, but they all show basically the same thing - startup in main, run their function, clean up, and then the program exits.
I don't know the proper terminology, but I was looking for two maybe three functions to run simultaneously with a loop in main. I tried a small test program and was wondering if it's setup correctly.
A structure is used as the argument for each function.
int running = 1; // global variable
DWORD WINAPI function_1(LPVOID);
DWORD WINAPI function_2(LPVOID);
main() {
HANDLE hndThreads[2];
DWORD threadIDs[2];
[code].....
Right now, function_2 is just a copy of function_1's definition. Everything *appears* to do what I want, but is it setup correctly?
View 2 Replies
ADVERTISEMENT
Sep 24, 2014
I wrote out this coding to deal with template functions, but how would I invoke the function and to display the statements which im trying to invoke?
#include <cstdlib>
#include <iostream>
using namespace std;
#include <string>
[Code].....
View 2 Replies
View Related
Apr 10, 2013
I wrote code that finds the number of prime numbers in a range entered by the user. Now I'm attempting to make it run in parallel with the number of threads I assign it to have. I'm using blocking technique, so I'm assigning, in this scenario, 4 threads - 1/4 of the numbers in the range to the first array, and the next 1/4 of the numbers in the range to the next array and so on. Then I want to execute the prime number counting code in parallel. I'm using openMP to do this. I'm having difficulty setting it all up properly. I have a little experience with pthreads but little with openMP and am struggling in how this should be done.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
[Code]....
View 2 Replies
View Related
Feb 12, 2013
I use g++ compiler and need some tips on how to get started with making a c++ GUI. The project I have will need a gui that will allow the user to conveniently invoke executable (the command-line args they take are long and inconvenient to enter manually) and basically print their output streams into a control (e.g. read only textbox). There will be a few graphics involved too (the input for those graphics will be from a file), so I'll need a library that will allow drawLine methods (nothing too fancy, I don't need a gaming library).
Making a GUI in c++. In Java the ability to make a GUI is a part of the native library, so the c++ way seems foreign to me.
View 2 Replies
View Related
Jun 13, 2013
I'm wondering if there is a library for C++ that supports threading over the network, maybe with a threading pool and a specific protocol; or if there is just a de-facto protocol for doing threading over the network.
View 10 Replies
View Related
May 13, 2014
I've written some code that I am currently threading but I am unsure how to pass by reference, or rather why my pass by reference is failing.
I am passing an array of floats by reference, this works fine when not threaded but I am given the error that float*&field does not match std::reference_wrapper<float*>.
Anyways. Here's the code.
The method:
diffuse(int b, float*& field, float*& pField, float diffFactor, float dt, int iteration)
The thread:
std::thread diffuseThread(diffuse, 1, std::ref(u), std::ref(prevU), visc, dt, iteration);
View 14 Replies
View Related
Nov 1, 2013
I want to make a thread outside an int main() method; and this code below gives an error of (paraphrasing) 'no constructor found for thread for type void()'
#include <thread>;
class Board(){
//Lines Later
[Code]....
Is there any way to accomplish threading outside the main and in a class?
View 1 Replies
View Related
Nov 26, 2012
I am trying to build a web application that uses p/invoke to access a method. Similiar to the one used here - [URL] However I keep getting the following error message - Unable to load DLL 'MyDll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) I read say to change the output directory to match that of the Mydll directory I have tried that and it still does not work.
View 6 Replies
View Related
Jun 25, 2014
I am having problems invoking methods in a nested class using reflection. I have the following:
A parent class, Group, that holds instances of a simple class called Signal. I want to modify the number of instances inside the group class often. So, all my code has to be dynamic and use reflection to know how many instances of signal there are inside the Group class.
class Group{
public static Signal name1 { get; set; }
public static Signal name2 { get; set; }
public static Signal name3 { get; set; }
[Code]....
I had no luck invoking the method of the instances of signal class that are inside the Group class. I tried getting the methods name using getMethods() but could not navigate through the syntax.
How could I invoke and pass parameters to the method of the instances of signal using reflection? Is there a better way of accessing the properties and methods of nested classes?
View 4 Replies
View Related
Jul 25, 2014
I want to override the operator new in a class, as follows:
class CMyclass
{
public:
void* operator new(size_t);
void operator delete(void*);
[Code]....
However, in debug version, whenever MemoryManager.Alloc(size) returns NULL, which means alloation fails, the AfxThrowMemoryException will cause the following exception:
Access violation reading location 0x#######
View 2 Replies
View Related
May 6, 2013
I have main thread that creates an WebBrowser2 COM object. and i want to invoke JScript functions on it from another thread. i try to use GIT but still doesn't work for me.. there is a problem with marshal WebBrowser2 for JScript?
View 4 Replies
View Related
Mar 25, 2013
I need do read a text file from memory card using threading.
I am able to do it with out using thread but because of some limitations, i have to do it with threading only.
Here is the code to read file from memory card with out thread:
Code: ##########################################
#include <stdio.h>
#include <stdlib.h>
#include <XMC4500.h> /* SFR declarations of the selected device */
#include <DAVE3.h> /* Declarations from DAVE3 Code
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
[Code]...
View 4 Replies
View Related
May 11, 2014
In our Qt application, we have to load several files on application start-up starting from a root directory.
We are recursively scanning the directories and loading the files.
The total time it takes is about 12 seconds. Can we reduce this time if we use multi-threading?
I have heard that multi-threading does not work everywhere and it increases code complexity and debugging issues.
Would multi-threading solve the above problem? We want it to be platform independent (Mac, Linux, Windows).
View 9 Replies
View Related
Feb 25, 2013
When I put boost::thread Thread; in my struct I get the error error C2248: 'boost::thread::thread' : cannot access private member declared in class 'boost::thread'
The whole struct is
struct Player {
bool key[256];
char nameString[64];
bool lMouseButton;
bool rMouseButton;
double mouseX;
double mouseY;
[Code] ....
View 1 Replies
View Related
Feb 10, 2012
I have a SSD and I am trying to use it to simulate my program I/O performance, however, IOPS calculated from my program is much much faster than IOMeter.
My SSD is PLEXTOR PX-128M3S, by IOMeter, its max 512B random read IOPS is around 94k (queue depth is 32). However my program (32 windows threads) can reach around 500k 512B IOPS, around 5 times of IOMeter!!! I did data validation but didn't find any error in data fetching. It's because my data fetching in order?
I paste my code belwo (it mainly fetch 512B from file and release it; I did use 4bytes (an int) to validate program logic and didn't find problem).
#include <stdio.h>
#include <Windows.h>
/*
** Purpose: Verify file random read IOPS in comparison with IOMeter
**/
//Global variables
long completeIOs = 0;
long completeBytes = 0;
int threadCount = 32;
unsigned long long length = 1073741824; //4G test file
[Code] ....
View 1 Replies
View Related
May 21, 2014
I have been working a project in C++. I have TTTMain.cpp file that has all the function calls, TTTFuntions.cpp that has all the functions, I have TTT.h file that has all the prototypes and variables and additionally I have Winner.h that has enum class Winner declaration in it. Here is my block of codes:
Winner.h file:
#ifndef winner
#define winner
enum class Winner {
[Code]....
My question is when I compile this gives me error on
Winner gameSquares[] = { Empty, Empty,Empty, Empty, Empty, Empty, Empty, Empty, Empty };
with saying "invalid use of non-static data data member" and It says "Empty was not declared in this scope."
I know calling enum is very very trick.
View 3 Replies
View Related
Aug 26, 2014
I am unable to get the string seaching function to work. it always says "Nothing found" I am stumped.
Code:
#include <stdio.h>
#include <string.h>
char tracks[][80] = {
"I left my Heart at Harvard Medical School",
"Newark, Newark, You suck balls",
"Dancing with a dork",
"From Here to maternity",
"The Girl from Iwo Jima",
[Code]....
View 3 Replies
View Related
Mar 5, 2013
1. In main() in the while loop entering 'q' does not exit.
2. in the isbnValidation function. The size check for minimum length words will work, but the check for "Invalid ISBN character" doesn't.
3. Every ISBN entered returns as valid. The formula is supposed to multiply the first digit by 10, next by 9 etc (skipping all the dashes) and add up to a weighted total which if valid will divide evenly by 11.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ISBNMIN 10
#define ISBNMAX 14
[Code].....
View 4 Replies
View Related
Mar 30, 2013
I have a file which I am unable to read properly. This is binary file so my code is"
#include<iostream>
#include<fstream>
#include<string>
[Code]....
the file to read can be download from this link :
[URL]
The first content of file should be 1 I am missing few content from the begining
View 2 Replies
View Related
Oct 31, 2013
so i have a container called Source that is of type std::map<std::string, std::vector<std::string>> but when I try to print it with this function:
void Lexer::PrintSource()
{
for(auto Iterator = this->Source.begin(); Iterator != this->Source.end(); Iterator++)
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
cout<< *SubIterator << endl;
}
i get these errors:
Lexer.cpp: In member function 'void Lexer::PrintSource()':
Lexer.cpp:29:42: error: 'struct std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >' has no member named 'begin'
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
^
Lexer.cpp:29:76: error: 'struct std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >' has no member named 'end'
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
^
View 6 Replies
View Related
Sep 29, 2014
#include <iostream>
using namespace std;
int main()
{
[Code]...
For some reason, I get the cout of
321
1
Because of the a++, shouldn't it be
321
2
The second example is what the cout needs to be.
View 1 Replies
View Related
Sep 2, 2013
If I were to exit a program, is it okay if I fail to properly destroy whatever structures were allocated?
For instance, if I do this with SDL, there might be obvious consequences like a dead window perhaps. But if I do this with a POD structure, is it okay?
Also, I realize RAII is supposed to fix parts of this but it's not perfect when environments are suddenly cut off with something like exit(1/0);
View 2 Replies
View Related
Apr 11, 2014
I'm parsing an xml file full of payslips and using the data in another application. I've got it all working but I suspect it isn't the most elegant piece of code. I run through the xml file finding a series of "Text" attributes/elements" and then I run through it again finding a series of "Field" attributes/elements, Here is a sample of the code:
For getting the "Text" fields :
XNamespace ns = "urn:crystal-reports:schemas:report-detail";
//
// Get all the Text attributes & Elements
//
foreach (XElement xtxt in xdoc.Descendants(ns + "Text"))
[Code]...
This works fine, I extract all the data I'm interested in and go to do my thing with it. However I really need to know when each record ends and I was doing that by looking for "Text24" in the text fields and "EeRef2" in the field fields, which wasn't very elegant in the first place. Then a "Text16" was added to end of each record which was fine I could just look for "Text16" but now it's apparent that "Text16" isn't always there. I've got it all working for now but I'd prefer to process one record at a time i.e. extract all the "Text" & "Field" values for one record, do whatever I need to do with it, update the xml file to indicate this progress ( if possible ) and then move on to the next record. I've attached a sample of the xml but basically is has the following structure :
<Details>
<Section>
<Text></Text>
"
<Field></Field>
[Code]...
So a record is everything between the first <Details> and the last </Details> with two <Details> and two </Details> in between.
View 2 Replies
View Related
Apr 4, 2013
#include <iostream>
using namespace std;
int function(int a,int b) {
return a + b;
} bool function2(int a,int b)
[Code] .....
View 3 Replies
View Related
May 21, 2013
This is my code for submitting students but when i use search function the course member is empty
Code: #include "windows.h"
#include "iostream"
#include <io.h>
#include <sstream>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#define SIZE 5
using std::cout;
using std::cin;
using namespace std;
int menu();
[code].....
View 11 Replies
View Related
Apr 9, 2013
I' used to check if my streams are properly opened with a simple:
Code:
if (!file)
std::cout<<"Error message";
However I just realized this is only a valid test if they are initialized with a wrong string and when I tested it:
Code:
int main() {
ifstream f_one("input.txt");
ifstream f_two("not_a_file.txt");
ifstream f_three;
[Code] ....
I didn't get the f_three error message, only the f_two.
how should I check my streams in order to prevent this?
View 6 Replies
View Related